﻿using System;
using System.Data;
using BLL;
using DAL;
using Ext.Net;
using Roles;

namespace ERP.Modules
{
    public partial class Mail_Setting : System.Web.UI.UserControl
    {
        #region[Define]
        private static bool _flag;
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                CheckPermisstion();
                GetMail();
                btnEdit.Disable(true);
                btnDel.Disable(true);
                btnExport.Disable(true);
            }
        }
        //Store
        protected void StoreMail_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            GetMail();
        }
        //Button
        protected void btnAddNew_Click(object sender, DirectEventArgs e)
        {
            _flag = true;
            ClearText();
            WindowsMail.Show();
        }
        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            var sm = grMail.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (var row in sm.SelectedRows)
                {
                    _flag = false;
                    GetMailEdit(int.Parse(row.RecordID));
                    WindowsMail.Show();
                }
        }
        protected void btnDel_Click(object sender, DirectEventArgs e)
        {
            try
            {
                var sm = grMail.GetSelectionModel() as CheckboxSelectionModel;
                if (sm != null)
                    foreach (var row in sm.SelectedRows)
                    {
                        var obj = new EtMail { ID = int.Parse(row.RecordID) };
                        BllMail.Delete(obj);
                    }
                GetMail();
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        protected void btnSavePopUp_Click(object sender, DirectEventArgs e)
        {
            Insert();
        }
        protected void btnCancelPopUp_Click(object sender, DirectEventArgs e)
        {
            ClearText();
            WindowsMail.Close();
        }
        //Double-Click
        protected void MailDblClick(object sender, DirectEventArgs e)
        {
            _flag = false;
            ClearText();
            int ID_Mail = int.Parse(e.ExtraParams["ID"]);
            GetMailEdit(ID_Mail);
            WindowsMail.Show();
        }
        //Search
        protected void Search_Event(object sender, DirectEventArgs e)
        {
            var _clsGetMail = new BllMail();
            var _dtSearch = _clsGetMail.GetMail(txtSearch.Text);
            if (_dtSearch != null && _dtSearch.Rows.Count > 0)
            {
                StoreMail.DataSource = _dtSearch;
                StoreMail.DataBind();
            }
            else
            {
                grMail.GetStore().RemoveAll();
            }
        }
        #endregion

        #region[Method]
        private void GetMail()
        {
            var _clsMail = new BllMail();
            var _dtMail = _clsMail.GetMail(string.Empty);
            if (_dtMail != null && _dtMail.Rows.Count > 0)
            {
                StoreMail.DataSource = _dtMail;
                StoreMail.DataBind();
            }
            else
            {
                grMail.GetStore().RemoveAll();
            }
        }
        private void GetMailEdit(int id)
        {
            var _clsGetMailEdit = new BllMail();
            var _dtGetMailEdit = _clsGetMailEdit.GetMailEdit(id);
            if (_dtGetMailEdit != null && _dtGetMailEdit.Rows.Count > 0)
            {
                txtID.Text = _dtGetMailEdit.Rows[0]["ID"].ToString();
                txtEmail.Text = _dtGetMailEdit.Rows[0]["Email"].ToString();
                txtPasswords.Text = _dtGetMailEdit.Rows[0]["Passwords"].ToString();
                txtPort.Text = _dtGetMailEdit.Rows[0]["Port"].ToString();
                txtHost.Text = _dtGetMailEdit.Rows[0]["Host"].ToString();
                txtContent.Text = _dtGetMailEdit.Rows[0]["Content"].ToString();
                txtSubjects.Text = _dtGetMailEdit.Rows[0]["Subjects"].ToString();
                if ((bool)_dtGetMailEdit.Rows[0]["IsActive"])
                {
                    chkIsActive.Checked = true;
                }
                else
                {
                    chkIsActive.Checked = false;
                }
            }
        }
        private bool CheckInsert()
        {
            bool Check = false;
            var _clsGetMail = new BllMail();
            var _dtGetMailCheck = _clsGetMail.GetMail(string.Empty);
            if (_dtGetMailCheck != null && _dtGetMailCheck.Rows.Count > 0)
            {
                foreach (DataRow dr in _dtGetMailCheck.Rows)
                {
                    if (txtID.Text != dr["ID"].ToString())
                    {
                        if (txtEmail.Text == dr["Email"].ToString())
                        {
                            Check = true;
                            HTML.clsFormat.Alert_ERROR("Thông Báo", "Tài khoản Mail này đã tồn tại!");
                        }
                    }
                }
            }
            return Check;
        }
        private void Insert()
        {
            if (_flag)
            {
                InsertValue();
            }
            else
            {
                if (CheckPermisstionDobclick())
                {
                    HTML.clsFormat.Alert_ERROR("Thông Báo", "Bạn không được cấp quyền để sửa đổi !");
                }
                else
                {
                    UpdateValue();
                }
            }
        }
        private void InsertValue()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new EtMail
                    {
                        Email = txtEmail.Text,
                        Passwords = txtPasswords.Text,
                        Port = int.Parse(txtPort.Text),
                        Host = txtHost.Text,
                        Content = txtContent.Text,
                        Subjects = txtSubjects.Text,
                        IsActive = chkIsActive.Checked
                    };
                    BllMail.Insert(obj);
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Thêm mới thành công !");
                    GetMail();
                    ClearText();
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.ToString());
            }
        }
        private void UpdateValue()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new EtMail
                    {
                        ID = int.Parse(txtID.Text),
                        Email = txtEmail.Text,
                        Passwords = txtPasswords.Text,
                        Port = int.Parse(txtPort.Text),
                        Host = txtHost.Text,
                        Content = txtContent.Text,
                        Subjects = txtSubjects.Text,
                        IsActive = chkIsActive.Checked
                    };
                    BllMail.Update(obj);
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Cập nhật thành công !");
                    GetMail();
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.ToString());
            }
        }
        #endregion

        #region[ClearText]
        private void ClearText()
        {
            FormPanelMail.Reset();
        }
        #endregion

        #region[Permisstion]
        private void CheckPermisstion()
        {
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.MailSetting, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnAdd == false)
                {
                    btnAddNew.Visible = false;
                }
                if (Is_BtnEdit == false)
                {
                    btnEdit.Visible = false;
                }
                if (Is_BtnView == false)
                {
                    PanelMail.Visible = false;
                }
                if (Is_BtnDel == false)
                {
                    btnDel.Visible = false;
                }
                if (Is_BtnPrint == false)
                {
                    btnExport.Visible = false;
                }
            }
        }
        private bool CheckPermisstionDobclick()
        {
            var check = false;
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.MailSetting, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnEdit == false)
                {
                    check = true;
                }
            }
            return check;
        }
        #endregion
    }
}